package com.example.yuejianapp14.Activity.Activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.yuejianapp14.Activity.Fragment.ClassFragment;
import com.example.yuejianapp14.Activity.Fragment.ClubFragment;
import com.example.yuejianapp14.Activity.Fragment.MainFragment;
import com.example.yuejianapp14.Activity.Fragment.TieziFragment;
import com.example.yuejianapp14.Activity.Fragment.UserFragment;
import com.example.yuejianapp14.R;




/**
 * Created by lzj on 2017/4/13.
 */

public class MainActivity extends FragmentActivity implements View.OnClickListener
{
    private LinearLayout class1;
    private LinearLayout club;
    private LinearLayout main;
    private LinearLayout tiezi;
    private LinearLayout user;

    private ImageButton mclass1;
    private ImageButton mclub;
    private ImageButton mmain;
    private ImageButton mtiezi;
    private ImageButton muser;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        setSelect(2);
    }

    private void initEvent()
    {
        class1.setOnClickListener(this);
        club.setOnClickListener(this);
        main.setOnClickListener(this);
        tiezi.setOnClickListener(this);
        user.setOnClickListener(this);
    }

    private void initView()
    {
        class1 = (LinearLayout) findViewById(R.id.id_class);
        club = (LinearLayout) findViewById(R.id.id_club);
        main = (LinearLayout) findViewById(R.id.id_main);
        tiezi = (LinearLayout) findViewById(R.id.id_tiezi);
        user = (LinearLayout) findViewById(R.id.id_user);

        mclass1 = (ImageButton) findViewById(R.id.class1);
        mclub = (ImageButton) findViewById(R.id.club);
        mmain = (ImageButton) findViewById(R.id.main);
        mtiezi = (ImageButton) findViewById(R.id.tiezi);
        muser = (ImageButton) findViewById(R.id.user);
    }

    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new ClassFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else
                {
                    transaction.show(mTab01);
                }
                mclass1.setImageResource(R.drawable.class_press);
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new ClubFragment();
                    transaction.add(R.id.id_content, mTab02);
                } else
                {
                    transaction.show(mTab02);
                }
                mclub.setImageResource(R.drawable.club_press);
                break;
            case 2:
                if (mTab03 == null)
                {
                    mTab03 = new MainFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else
                {
                    transaction.show(mTab03);
                }
                mmain.setImageResource(R.drawable.main_press);
                break;
            case 3:
                if (mTab04 == null)
                {
                    mTab04 = new TieziFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else
                {
                    transaction.show(mTab04);
                }
                mtiezi.setImageResource(R.drawable.tiezi_press);
                break;
            case 4:
                if (mTab05 == null)
                {
                    mTab05 = new UserFragment();
                    transaction.add(R.id.id_content, mTab05);
                } else
                {
                    transaction.show(mTab05);
                }
                muser.setImageResource(R.drawable.user_press);
                break;
            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }
        if (mTab05 != null)
        {
            transaction.hide(mTab05);
        }
    }

    @Override
    public void onClick(View v)
    {
        resetImgs();
        switch (v.getId())
        {
            case R.id.id_class:
                setSelect(0);
                break;
            case R.id.id_club:
                setSelect(1);
                break;
            case R.id.id_main:
                setSelect(2);
                break;
            case R.id.id_tiezi:
                setSelect(3);
                break;
            case R.id.id_user:
                setSelect(4);
                break;

            default:
                break;
        }
    }


    private void resetImgs()
    {
        mmain.setImageResource(R.drawable.main);
        mclass1.setImageResource(R.drawable.class1);
        mclub.setImageResource(R.drawable.club);
        mtiezi.setImageResource(R.drawable.tiezi);
        muser.setImageResource(R.drawable.user);
    }

}